package com.hope.hotel.controller;

import com.hope.hotel.common.Result;
import com.hope.hotel.entity.dto.BookingDTO;

import com.hope.hotel.service.BookingService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 预订管理控制器
 */
@RestController
@RequestMapping("/booking")
@RequiredArgsConstructor
public class BookingController {

    private final BookingService bookingService;
    
    /**
     * 订单状态常量定义
     */
    public static class BookingStatus {
        public static final String BOOKED = "booked";         // 已下单（需要通过payStatus判断是否支付）
        public static final String CHECKED_IN = "checked_in"; // 已入住
        public static final String CONFIRMED = "confirmed";   // 已完成（已退房）
        public static final String CANCELLED = "cancelled";   // 已取消（未支付时才能取消）
    }

    /**
     * 创建预订
     */
    @PostMapping("/create")
    public Result createBooking(@RequestBody BookingDTO bookingDTO, HttpServletRequest request) {
        // 从请求中获取用户ID
        String userId = (String) request.getAttribute("userId");
        if (userId == null) {
            return Result.error("用户未登录");
        }
        bookingDTO.setUserId(userId);
        return bookingService.createBooking(bookingDTO);
    }

    /**
     * 获取用户的所有预订
     */
    @GetMapping("/user-bookings")
    public Result getUserBookings(HttpServletRequest request) {
        // 从请求中获取用户ID
        String userId = (String) request.getAttribute("userId");
        if (userId == null) {
            return Result.error("用户未登录");
        }
        return bookingService.getUserBookings(userId);
    }

    /**
     * 获取预订详情
     */
    @GetMapping("/{bookingId}")
    public Result getBookingDetail(@PathVariable String bookingId, HttpServletRequest request) {
        // 从请求中获取用户ID
        String userId = (String) request.getAttribute("userId");
        if (userId == null) {
            return Result.error("用户未登录");
        }
        return bookingService.getBookingDetail(bookingId, userId);
    }

    /**
     * 取消预订
     */
    @PostMapping("/cancel/{bookingId}")
    public Result cancelBooking(
            @PathVariable String bookingId,
            HttpServletRequest request) {
        // 从请求中获取用户ID
        String userId = (String) request.getAttribute("userId");
        if (userId == null) {
            return Result.error("用户未登录");
        }
        return bookingService.cancelBooking(bookingId, userId);
    }
    
    // 可以在BookingService中添加以下方法来支持这些功能
    // /**
    //  * 获取当前用户订单统计信息
    //  */
    // @GetMapping("/stats")
    // public Result getBookingStats(HttpServletRequest request) {
    //     // 从请求中获取用户ID
    //     String userId = (String) request.getAttribute("userId");
    //     if (userId == null) {
    //         return Result.error("用户未登录");
    //     }
    //     return bookingService.getBookingStats(userId);
    // }
    
    // /**
    //  * 检查用户是否已经评价过该订单
    //  */
    // @GetMapping("/check-reviewed/{bookingId}")
    // public Result checkIfReviewed(@PathVariable String bookingId, HttpServletRequest request) {
    //     // 从请求中获取用户ID
    //     String userId = (String) request.getAttribute("userId");
    //     if (userId == null) {
    //         return Result.error("用户未登录");
    //     }
    //     return bookingService.checkIfReviewed(bookingId, userId);
    // }

    /**
     * 获取酒店的所有预订（管理员使用）
     */
    @GetMapping("/hotel/{hotelId}")
    public Result getHotelBookings(
            @PathVariable String hotelId,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int pageSize,
            HttpServletRequest request) {
        // 从请求中获取管理员ID
        String adminId = (String) request.getAttribute("adminId");
        if (adminId == null) {
            return Result.error("管理员未登录");
        }
        return bookingService.getHotelBookings(hotelId, page, pageSize);
    }
    
    /**
     * 搜索酒店订单（管理员使用）
     */
    @GetMapping("/hotel/{hotelId}/search")
    public Result searchHotelBookings(
            @PathVariable String hotelId,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) String status,
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int pageSize,
            HttpServletRequest request) {
        // 从请求中获取管理员ID
        String adminId = (String) request.getAttribute("adminId");
        if (adminId == null) {
            return Result.error("管理员未登录");
        }
        return bookingService.searchHotelBookings(hotelId, keyword, status, startDate, endDate, page, pageSize, adminId);
    }
    
    /**
     * 管理员更新订单状态
     */
    @PostMapping("/hotel/update-status")
    public Result updateBookingStatus(
            @RequestParam String bookingId,
            @RequestParam String status,
            HttpServletRequest request) {
        // 从请求中获取管理员ID
        String adminId = (String) request.getAttribute("adminId");
        if (adminId == null) {
            return Result.error("管理员未登录");
        }
        return bookingService.updateBookingStatus(bookingId, status, adminId);
    }
    
    /**
     * 获取订单详情（管理员视角）
     */
    @GetMapping("/hotel/detail/{bookingId}")
    public Result getBookingDetailForAdmin(
            @PathVariable String bookingId,
            HttpServletRequest request) {
        // 从请求中获取管理员ID
        String adminId = (String) request.getAttribute("adminId");
        if (adminId == null) {
            return Result.error("管理员未登录");
        }
        return bookingService.getBookingDetailForAdmin(bookingId, adminId);
    }
    
    /**
     * 更新订单支付状态（管理员使用）
     */
    @PostMapping("/hotel/update-pay-status")
    public Result updatePayStatus(
            @RequestParam String bookingId,
            @RequestParam Integer payStatus,
            HttpServletRequest request) {
        // 从请求中获取管理员ID
        String adminId = (String) request.getAttribute("adminId");
        if (adminId == null) {
            return Result.error("管理员未登录");
        }
        return bookingService.updatePayStatus(bookingId, payStatus, adminId);
    }
    
    /**
     * 获取酒店订单统计数据（管理员使用）
     * 返回各种状态的订单数量统计
     */
    @GetMapping("/hotel/{hotelId}/stats")
    public Result getHotelBookingStats(
            @PathVariable String hotelId,
            HttpServletRequest request) {
        // 从请求中获取管理员ID
        String adminId = (String) request.getAttribute("adminId");
        if (adminId == null) {
            return Result.error("管理员未登录");
        }
        return bookingService.getHotelBookingStats(hotelId, adminId);
    }
    
    /**
     * 获取订单图表统计数据（管理员使用）
     * 返回订单数量和收入的时间序列数据
     */
    @GetMapping("/hotel/{hotelId}/chart-stats")
    public Result getOrderChartStatistics(
            @PathVariable String hotelId,
            @RequestParam(required = false, defaultValue = "month") String period,
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate,
            HttpServletRequest request) {
        // 从请求中获取管理员ID
        String adminId = (String) request.getAttribute("adminId");
        if (adminId == null) {
            return Result.error("管理员未登录");
        }
        return bookingService.getOrderChartStatistics(hotelId, period, startDate, endDate, adminId);
    }
    
    /**
     * 获取房型订单分布统计（管理员使用）
     */
    @GetMapping("/hotel/{hotelId}/room-type-stats")
    public Result getRoomTypeBookingStats(
            @PathVariable String hotelId,
            HttpServletRequest request) {
        // 从请求中获取管理员ID
        String adminId = (String) request.getAttribute("adminId");
        if (adminId == null) {
            return Result.error("管理员未登录");
        }
        return bookingService.getRoomTypeBookingStats(hotelId, adminId);
    }

} 